// Project: fcounter_2pic2550_osc
// FileName: fcounter_2pic2550_osc

#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>


#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = ECH
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF

#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)

unsigned char N;


void high(void);
void low(void);

#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code



void high(){


if(INTCONbits.INT0IF){

TMR1L = 93;
PORTBbits.RB6 = 1;

T1CONbits.TMR1ON = 1;
INTCONbits.INT0IF = 0;
}



if(PIR1bits.TMR1IF){
N++;


if(N > 195){

PORTBbits.RB7 = 1;
Delay1TCY();Delay1TCY();Delay1TCY();
PORTBbits.RB6 = 0;
Delay1TCY();
PORTBbits.RB7 = 0;

N = 0;
T1CONbits.TMR1ON = 0;
TMR1H = 176;
}
PIR1bits.TMR1IF = 0;
}
}

void low(){
}



void UserInit(void){

TMR1H = 0; TMR1L = 0;
PORTBbits.RB7 = 0;
PORTBbits.RB6 = 1;
N = 0;
T1CONbits.TMR1ON = 1;
}



void main(void){

PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b01000001;
ANSEL = 0b00000000;
ANSELH = 0b00000000;


OpenTimer1(
TIMER_INT_ON &
T1_16BIT_RW &
T1_SOURCE_INT &
T1_PS_1_1 &
T1_OSC1EN_OFF &
T1_SYNC_EXT_ON
);
IPR1bits.TMR1IP = 1;
PIR1bits.TMR1IF = 0;

INTCONbits.INT0IE = 1;
INTCONbits.INT0IF = 0;


RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 0;

UserInit();


while(1){
}
}